<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-date-picker type="datetime" placeholder="开始时间" v-model="filters.start_time"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-date-picker type="datetime" placeholder="结束时间" v-model="filters.end_time"></el-date-picker>
				</el-form-item>
				<el-form-item>
					<el-select @change="getNewCode" v-model="filters.role" clearable placeholder="角色类型">
						<el-option
			  				v-for="item,index in roles"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-select v-model="filters.position" clearable placeholder="角色职位">
						<el-option
			  				v-for="item,index in positionsById"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="输入角色类型或角色职位名称搜索"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getPositions">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="positions" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="index" label="序号" align="center" width="100">
			</el-table-column>
			<el-input prop="id" type="hidden"></el-input>
			<el-input prop="role_id" type="hidden"></el-input>
			<el-table-column prop="role_name" align="center" label="角色类型" >
			</el-table-column>
			<el-table-column prop="name" align="center" label="角色职位" >
			</el-table-column>
			<el-table-column prop="create_date" align="center" label="创建时间" :formatter="formatDateTime">
			</el-table-column>
			<el-table-column align="center" label="操作">
				<template slot-scope="scope">
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
					<el-button size="small" @click="handleAuthority(scope.$index, scope.row)">权限分配</el-button>
				</template>
			</el-table-column>
		</el-table>
		<div class="toolbar" style="display: flex;justify-content: flex-end;">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				:current-page="pageIndex"
				:page-sizes="[10, 20, 30, 40]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total">
			</el-pagination>
		</div>
		<!--新增界面-->
		<el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="角色类型" prop="role">
					<el-select v-model="addForm.role" clearable placeholder="角色类型">
						<el-option
			  				v-for="item,index in roles"
			  	      			:label="item.name"
			  	      			:value="item.id"
								:key="index">
						</el-option>
					</el-select>
				</el-form-item>
			</el-form>
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="角色职位" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import moment from '../../common/js/moment'
	import { getRoleList,getPositionList,addPosition,removePosition,queryResource } from '../../api/api';

	export default {
		data() {
			let varIsRole = (rule, value, callback) => {
				if(value === ''){
					callback(new Error('请选择类型'));
				}else{
					callback();
				}
			};
			return {
				filters: {
					name: ''
				},
				roles: [],
				positions: [],
				positionsById: [],
				total: 0,
				page: 1,
				pageIndex: 1,
				pageSize: 10,
				listLoading: false,
				sels: [],//列表选中列

				//新增界面数据
				addForm: {
					name: '',
					roleId:''
				},
				addFormVisible: false,//新增界面是否显示
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入名称', trigger: 'blur' }
					]
				}
				
			}
		},
		methods: {
			formatDateTime: function (row, column){
				var date = row[column.property];
				if (date == undefined) {
		            		return "";
		        	}
				return this.moment(date).format("YYYY-MM-DD HH:mm:ss");
			},
			handleCurrentChange(val) {
				this.pageIndex = val;
				this.getPositions();
			},
			handleSizeChange(val) {
				this.pageSize = val;
				this.getPositions();
			},
			//获取角色职位列表
			getPositions() {
				let auth = localStorage.getItem('authority');
				let positionIds = JSON.parse( localStorage.getItem('positionIds'));
				let para = {
					authorityNow: auth,
					positionIds: positionIds,
					pageIndex: this.pageIndex,
					pageSize: this.pageSize,
					keyword: this.filters.keyword,
					startTime: this.filters.start_time,
					endTime: this.filters.end_time,
					role: this.filters.role,
					position: this.filters.position
				};
				this.listLoading = true;
				getPositionList(para).then((res) => {
					console.log(res.data);
					this.total = res.data.pageCount;
					this.positions = res.data.data;
					this.positionsById = res.data.data;
					this.listLoading = false;
				});
			},
			//新增  获取当前下拉框选中值的value代码值
			getNewCode(value) {
				this.newRole = value;
				this.getPositionsById(this.newRole);
			},
			//获取角色类型--搜索
			getRoles() {
				let auth = localStorage.getItem('authority');
				let role = String(localStorage.getItem('role'));
				let para = {
					authority: auth,
					roleIds: role
				};
				this.listLoading = true;
				getRoleList(para).then((res) => {
					this.roles = res.data.data;
					this.listLoading = false;
				});
			},
			//获取角色职位
			getPositionsById() {
				let auth = localStorage.getItem('authority');
				let positionIds = JSON.parse( localStorage.getItem('positionIds'));
				let para = {
					authorityNow: auth,
					positionIds: positionIds,
					role: this.filters.role,
				};
				this.listLoading = true;
				getPositionList(para).then((res) => {
					this.positionsById = res.data.data;
					this.listLoading = false;
				});
			},
			
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					let para = { id: row.id };
					removePosition(para).then((res) => {
						this.listLoading = false;
						if(res.code == 0) {
							this.$message({
								message: '删除成功',
								type: 'success'
							});
							this.getPositions();
						} else {
							this.$message({
								message: res.message,
								type: 'error'
							});
						}
					}).catch(() => {
						this.listLoading = false;
					});
				}).catch(() => {

				});
			},
			//显示新增界面
			handleAdd: function () {
				this.addFormVisible = true;
			},
			//跳转到权限分配页面
			handleAuthority: function (index, row) {
				this.$router.push({ path: '/admin/authority',query:{id:row.id} });
			},
			
			//新增
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							let para;
							if(this.addForm.role){
								para = {
									roleId: this.addForm.role,
									name: this.addForm.name
								};
							}else{
								this.$message({
									message: '请选择角色类型',
									type: 'error'
								});
							}
							this.addLoading = true;
							console.log(para);
							addPosition(para).then((res) => {
								this.addLoading = false;
									this.$message({
										message: '添加成功',
										type: 'success'
									});
									this.$refs['addForm'].resetFields();
									this.addFormVisible = false;
									this.getPositions();
							});
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
		},
		mounted() {
			this.getRoles();
			this.getPositions();
		}
	}

</script>

<style scoped>

</style>
